<?php
namespace app\index\controller;
use app\common\controller\Base;
use app\common\model\User;
use myqq\Url;
use think\Controller;
use myqq\Qc;
class Login extends Base
{
//    博客系统登录页面
    public function index()
    {
        //使用缓存来记录错误登录次数
        //$logincount=session('retry');
        //使用缓存来记录错误登录次数
        //$logincount =cache('retry');
        $logincount =cookie('codecount');
        $dcode=decrypt(cookie('codecount'),SALT);
        if($logincount === NULL || !is_numeric($dcode)){
            //session('retry',1);
            cookie('codecount',encrypt('1',SALT),3600);
        }
        if(Base::isLogin()){
            $this->redirect('blog/Index/index');
        }
        if(request()->isAjax()){
            $data=input('post.','','trim');
            $res=User::login($data);
            return json($res);
        }
        $this->assign('logincount',decrypt(cookie('codecount'),SALT));
        return view();
    }
//    博客系统用户注册页面
    public function reg(){
        if(request()->isAjax()){
            $data=input('post.','','trim');
            $result=User::userreg($data);
//            if($result['code']==1){
//                $this->success($result['msg'],'index/Index/index');
//            }
//            $this->error($result['msg']);
            //返回注册状态
            return json($result);
        }
        return view();
    }

    public function active($token){
        if(isset($token)){
            $res=User::active($token);
            if($res['code']==1){
                $this->success($res['msg'],'index/Index/index');
            }
            $this->success($res['msg'],'index/Index/reg');
        }
    }

    /**
     * QQ授权登录
     */
    public function qqlogin(){
        $qc = new Qc();
        $qc->qq_login();
    }

    /**
     * QQ回调地址
     */
    public function qqcallback(){
        $qc = new Qc();
        $access_token=$qc->qq_callback();
        $openid=$qc->get_openid();
        $qc = new Qc($access_token,$openid);
        $userinfo=$qc->get_user_info();
//        $qqface=file_get_contents($userinfo['figureurl_1']);
        $url=$userinfo['figureurl_1'];
        $urlclass = new Url();
        $qqface = $urlclass->get_contents($url,1);
        $qqfile=ROOT_PATH."public/face/".md5(time()).".jpg";
        file_put_contents($qqfile,$qqface);
        $data=[
            'userkey'=>$openid,
            'nickname'=>$userinfo['nickname'],
            'faceimg'=>$qqfile,
        ];
//        把用户有用的基本信息写入session
        session('userinfo',$data);
//        判断用户是否是老用户
        $qquser= db('users')->where('userkey',$openid)->find();
        if($qquser){
            //查询绑定账户
            $user=db('user')->where('uid',$qquser['uid'])->field('account')->find();
//            设置为登录状态
            $result=Base::setLogin($user['account']);
            if($result['code']==1){
                $this->redirect('blog/Index/index');
            }
        }
        //如果是新用户
        $this->redirect('index/Index/bind');
    }

    /**
     * 账号绑定
     */
    public function bind(){
        if(request()->isAjax()){
            $data=input('post.');
            return json(User::bind($data));
        }
        return view();
    }

    /**
     * 退出
     */
    public function goout(){
        cookie('name', null);
        cookie('code', null);
        $this->redirect('index/index/index');
    }
}
